package com.sam.example4.client;

import java.net.InetSocketAddress;
import java.nio.charset.Charset;

import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

public class ClientTest {
	public static void main(String[] args) {
		NioSocketConnector connector = new NioSocketConnector();
		
		connector.getFilterChain().addLast("logger", new LoggingFilter());
		connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("gb2312"))));
		
		connector.setConnectTimeoutMillis(1000);
		connector.setHandler(new IoHandlerAdapter(){
			@Override
			public void messageReceived(IoSession session, Object message) throws Exception {
				String msg = message.toString();
				System.out.println("客户端接收到的信息为：" + msg);
			}
		});
		
		ConnectFuture connect = connector.connect(new InetSocketAddress("127.0.0.1", 1235));
		connect.awaitUninterruptibly();
		connect.getSession().write("hello,测试！");
	}
}
